import {_decorator, Component, Node, Sprite, SpriteAtlas, Vec3} from 'cc';
import {CardRank, CardSuit, ClickEventType, SK_CARD_INDEX_KING_B, SK_CARD_INDEX_KING_S, SK_CS_CLUB, SK_CS_DIAMOND, SK_CS_HEART, SK_CS_KING_B, SK_CS_KING_S, SK_CS_SPADE} from '../GameDef';
import {BaseCard} from '../models/Card';
import EventCenter from '../event/EventCenter';
import {GameEvent} from '../event/GameEvent';


const { ccclass, property } = _decorator;

export enum Color {
    diamond,
    club,
    heart,
    spade,
    king_s,
    king_b,
    colorless,
    Red,
    Black
}

//与同城游Index对接用
export enum CardIndex {
    Two = 1,
    Three = 2,
    Four = 3,
    Five = 4,
    Six = 5,
    Seven = 6,
    Eight = 7,
    Nine = 8,
    Ten = 9,
    J = 10,
    Q = 11,
    K = 12,
    A = 13,
}


interface IUCard {
    row: number;
    col: number;

    uid: number;
    suit: CardSuit;
    rank: CardRank;
    rotation: number;

    init(row: number, col: number, type: ClickEventType): boolean;

    refresh(data: BaseCard): void;

    clear(): void;

}


@ccclass('UCard')
export default class UCard extends Component implements IUCard {
    row: number;
    col: number;
    uid: number;
    suit: CardSuit;
    rank: CardRank;
    rotation: number;
    type: ClickEventType;

    //"小花色"
    @property(Sprite)
    spSuits: Sprite = null;

    //"大花色"
    @property(Sprite)
    spSuit: Sprite = null;

    //"牌值"
    @property(Sprite)
    spRank: Sprite = null;

    //"牌值图集"
    @property(SpriteAtlas)
    atlas_value: SpriteAtlas = null;

    //"花色图集"
    @property(SpriteAtlas)
    atlas_shape: SpriteAtlas = null;

    @property(Node)
    front: Node = null;
    @property(Node)
    back: Node = null;
    @property(Node)
    selectUI: Node = null;

    get x(): number {
        return this.node.position.x;
    }

    get y(): number {
        return this.node.position.y;
    }

    set x(x: number) {
        if(x==undefined)
            return;
        this.node.setPosition(x, this.y);
    }
    set y(y: number) {
        if(y==undefined)return;
        this.node.setPosition(this.x, y);
    }

    get roation(): number {
        return this.rotation;
    }

    set isOpen(b: boolean) {
        if(b == undefined) return;
       this.front.active = b;
       this.back.active = !b;
    }

    get isOpen(): boolean {
        return this.front.active;
    }

    set roation(roation: number) {
        if( roation == undefined)
            return;
        this.rotation = roation;
        const euler = new Vec3(0, 0, -roation);
        this.node.eulerAngles = euler;
    }


    init(row?: number, col?: number, type: ClickEventType = ClickEventType.NONE): boolean {
        this.row = row;
        this.col = col;
        this.type = type;
        return true;
    }
    refresh(data): void {
        this.uid = data.uid;
        this.suit = data.suit;
        this.rank = data.rank;
        this.roation = data.rotation;
        this.isOpen = data.isOpen;
        this.x = data.x;
        this.y = data.y;
        this.updateUi(data);
    }

    updateUi(data: any) {
        let shape = 3-data.suit;

        let index = this.cardRankToUIRank(data.rank);
        if (this.spSuits) {
            if (shape < SK_CS_KING_S) {
                this.spSuits.enabled = true;
                this.spSuits.spriteFrame = this.atlas_shape.getSpriteFrame('color_' + shape + '_s');
            } else {
                this.isOpen = false;
            }
        }
        if (this.spSuit) {
            this.spSuit.spriteFrame = this.atlas_shape.getSpriteFrame('color_' + shape);
            if (index === SK_CARD_INDEX_KING_S) {
                this.spSuit.spriteFrame = this.atlas_shape.getSpriteFrame('color_4');
            } else if (index === SK_CARD_INDEX_KING_B) {
                this.spSuit.spriteFrame = this.atlas_shape.getSpriteFrame('color_5');
            }
        }
        switch (shape) {
            case SK_CS_DIAMOND:
            case SK_CS_HEART:
                this.spRank.spriteFrame = this.atlas_value.getSpriteFrame('red_' + index);
                break;
            case SK_CS_CLUB:
            case SK_CS_SPADE:
                this.spRank.spriteFrame = this.atlas_value.getSpriteFrame('black_' + index);
                break;
            case SK_CS_KING_S:
                this.spRank.spriteFrame = this.atlas_value.getSpriteFrame('black_joker');
                break;
            case SK_CS_KING_B:
                this.spRank.spriteFrame = this.atlas_value.getSpriteFrame('red_joker');
                break;
            default:
                break;
        }
    }
    onClick() {
        if (this.back.active)
            return;
        EventCenter.emit(GameEvent.CardClick, this.uid);
    }

    // 牌值转换UI值适配，非必要
    cardRankToUIRank(index: CardRank) {
        let rank: number = index;
        if (index == CardRank.ACE)
            rank = 13;
        return rank;
    }

    clear(): void {
        throw new Error('Method not implemented.');
    }

    select() {
        this.selectUI.active = true;
    }

    unselect() {
        this.selectUI.active = false;
    }

    fixCard(cardData: BaseCard) {
        this.rank = cardData.rank;
        this.suit = cardData.suit;
        this.uid = cardData.uid;
        this.updateUi(cardData);
    }
}